<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Dashboard</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            background-image: url('/static/images/1.jpg'); 
            background-size: cover;
            background-position: center;
            color: Black;
            padding: 20px;
        }
        .container {
            max-width: 1200px;
            margin: auto;
        }
        .button-container {
            display: flex;
            flex-wrap: wrap;
            gap: 10px;
            margin-bottom: 20px;
        }
        .button-container button {
            padding: 10px 20px;
            font-size: 16px;
            margin: 10px;
            cursor: pointer;
            background-color: #2575fc;
            border: none;
            color: white;
            border-radius: 5px;
            transition: background-color 0.3s;
        }
        .button-container button:hover {
            background-color: #45a049;
        }
        table {
            width: 100%;
            border-collapse: collapse;
            margin-top: 20px;
            background-color: rgba(255, 255, 255, 0.8);
            color: #333;
        }
        table, th, td {
            border: 1px solid #ddd;
        }
        th, td {
            padding: 8px;
            text-align: left;
        }
        th {
            background-color: #f2f2f2;
        }
    </style>
    <script>
        function sendMessage(topic, msg) {
            fetch('/back/publish/', {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json',
                    'X-CSRFToken': '{{ csrf_token }}'
                },
                body: JSON.stringify({'topic': topic, 'msg': msg})
            }).then(response => response.json())
              .then(data => console.log(data))
              .catch(error => console.error('Error:', error));
        }
    </script>
</head>
<body>
    <div class="container">
        <h1>智能家居中控台</h1>
        <div class="button-container">
            <button onclick="sendMessage('XINGHUAN/CURTAIN_KZ', '1')">开窗帘</button>
            <button onclick="sendMessage('XINGHUAN/CURTAIN_KZ', '2')">关窗帘</button>
            <button onclick="sendMessage('XINGHUAN/CURTAIN_KZ', '3')">自动模式</button>
            <button onclick="sendMessage('BOOK/WS2812B_kz', 'W255')">开灯带</button>
            <button onclick="sendMessage('BOOK/WS2812B_kz', 'W100')">关灯带</button>
            <button onclick="sendMessage('BOOK/WS2812B_kz', 'M6')">最爱模式</button>
            <button onclick="sendMessage('/readcard', '1')">读卡</button>
            <button onclick="location.href='{% url 'write_card' %}'">写卡</button>
        </div>

        <h2>读卡</h2>
        <table>
            <tr>
                <th>卡ID</th>
                <th>信息</th>
                <th>时间戳</th>
            </tr>
            {% for data in environment_data %}
            <tr>
                <td>{{ data.card_number }}</td>
                <td>{{ data.information }}</td>
                <td>{{ data.timestamp }}</td>
            </tr>
            {% endfor %}
        </table>

        <h2>已有卡信息</h2>
        <table>
            <tr>
                <th>卡号</th>
                <th>信息</th>
                <th>时间戳</th>
            </tr>
            {% for card in card_data %}
            <tr>
                <td>{{ card.card_number }}</td>
                <td>{{ card.information }}</td>
                <td>{{ card.timestamp }}</td>
            </tr>
            {% endfor %}
        </table>
    </div>
</body>
</html>
